package com.clippick.walkthrough;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

import co.kr.clippick.R;

public class TutorialActivity extends Activity {
	private Context mContext;
	private SharedPreferences pref;
	private String pagePath = "file:///android_asset/";
	private String[] pages = { "Page1.html", "Page2.html", "Page3.html",
			"Page4.html", "Page5.html", "Page6.html", "Page7.html",
			"Page8.html" };
	private String[] pagesKor = { "KPage1.html", "KPage2.html", "KPage3.html",
			"KPage4.html", "KPage5.html", "KPage6.html", "KPage7.html",
			"KPage8.html" };

	private ImageView image1, image2, image3, image4, image5, image6, image7,
			image8;
	private int[] imageIds = { R.id.through1, R.id.through2, R.id.through3,
			R.id.through4, R.id.through5, R.id.through6, R.id.through7,
			R.id.through8 };
	private ImageView[] indicators = { image1, image2, image3, image4, image5,
			image6, image7, image8 };
	private Button finishButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tutorial);
		mContext = this;
		pref = getSharedPreferences("Login_info", MODE_PRIVATE);
		pref.edit().putBoolean("first", true);
		pref.edit().commit();

		// system language
		String language = Locale.getDefault().getDisplayLanguage();
		Log.i("lang", language);

		List<String> pageList = new ArrayList<String>();
		for (int i = 0; i < pages.length; i++) {
			if (language.equals("한국어")) {
				pageList.add(pagePath + pagesKor[i]);
			} else {
				pageList.add(pagePath + pages[i]);
			}
			indicators[i] = (ImageView) findViewById(imageIds[i]);
		}
		indicators[0].setImageResource(R.drawable.indicator_high);

		finishButton = (Button) findViewById(R.id.finish_button);
		finishButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		ViewPager pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new HtmlPageAdapter(mContext, pageList));
		pager.setOnPageChangeListener(new MyPageChangeListener());
	}

	private int focusedPage = 0;

	private class MyPageChangeListener extends
			ViewPager.SimpleOnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			focusedPage = position;

			// set image by position
			for (int i = 0; i < indicators.length; i++) {
				if (i == position) {
					indicators[i].setImageResource(R.drawable.indicator_high);
				} else {
					indicators[i].setImageResource(R.drawable.indicator_low);
				}

			}
		}
	}
}
